/*
Copyright (c) 2009, Luke Berndt
All rights reserved.
 
 * More Information: http://www.tagflo.com

Redistribution and use in source and binary forms, with or without modification, are permitted 
provided that the following conditions are met:

* Redistributions of source code must retain the above copyright notice, this list of conditions 
  and the following disclaimer.

* Redistributions in binary form must reproduce the above copyright notice, this list of conditions 
  and the following disclaimer in the documentation and/or other materials provided with the distribution.

* Neither the name of RoboTastic Software nor the names of its contributors may be used to endorse or 
  promote products derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED 
WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A 
PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR 
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED 
TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
POSSIBILITY OF SUCH DAMAGE.
*/


using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Data.SqlServerCe;
using System.Data.OleDb;
using System.Data.SqlClient;
using System.Data;
using System.Linq;

namespace TagFlo
{
    /// <summary>
    /// Interaction logic for TagDialog.xaml
    /// </summary>

    
    public partial class TagDialog : System.Windows.Window
    {
        //private ThreadedDataSet sqlDataSet;
        //DataRowView currentTagSet;

        PhotoDBEntities photoEntities = new PhotoDBEntities();
        TagSet currentTagSet;
        

       
        private void RemoveTagButton_Click(object sender, RoutedEventArgs e)
        {
            Button b = sender as Button;  // sender is the button user clicked
            Tag tag = (Tag)b.DataContext;

            currentTagSet.Tags.Remove(tag);
            photoEntities.SaveChanges();

        }
        public void RemoveButton_Click(Object sender, RoutedEventArgs e)
        {
            if (TagSetListBox.SelectedItem != null)
            {
                TagSet selectedTagSet = (TagSet)TagSetListBox.SelectedItem;
                string message = "Are sure you want to remove the TagSet: " + selectedTagSet.TagSetName;
                MessageBoxButton buttons = MessageBoxButton.YesNo;
                MessageBoxResult result = MessageBox.Show(message, "Remove TagSet", buttons);

                if (result == MessageBoxResult.Yes)
                {
                    currentTagSet = null;
                    TagSetListBox.SelectedItem = null;
                    photoEntities.DeleteObject(selectedTagSet);
                    photoEntities.SaveChanges();             
                }

            }
        }

        private void AddButton_Click(Object sender, RoutedEventArgs e)
      {
            //if (!photoEntities.TagSets.Any(t=>t.TagSetName == TagSetTextBox.Text))
            {
                TagSet tagSet = TagSet.New();
                tagSet.TagSetName = TagSetTextBox.Text;
                currentTagSet = tagSet;
                photoEntities.AddToTagSets(tagSet);
                photoEntities.SaveChanges();
                TagSetListBox.DataContext = photoEntities.TagSets.Include("Tags");
      
            }
        }

        private void TagButton_PreviewKeyDown(object sender, System.Windows.Input.KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            {
                if (currentTagSet != null)
                {
                    Tag t = photoEntities.Tags.Where("it.TagName = '" + TagButtonComboBox.Text + "'").FirstOrDefault();

                         if (t == null)
                         {

                             t = TagFlo.Tag.New();
                             t.TagName = TagButtonComboBox.Text;
                         }
                    currentTagSet.Tags.Add(t);
                    photoEntities.SaveChanges();
                    TagButtonListBox.DataContext = currentTagSet.Tags;
                }
            }
           
        }

       
        public void TagSetListBox_SelectionChanged (Object sender,SelectionChangedEventArgs e)
        {

            if (TagSetListBox.SelectedItem != null)
            {
                if (TagSetListBox.SelectedItem.GetType() == typeof(TagSet))
                {

                    currentTagSet = (TagSet)TagSetListBox.SelectedItem;
                    TagButtonListBox.DataContext = currentTagSet.Tags;
                    
                }
            }
            else
            {
                currentTagSet = null;
                TagButtonListBox.DataContext = null;
            }
        }

       

        public TagDialog()
        {
            InitializeComponent();
            TagSetListBox.DataContext = photoEntities.TagSets.Include("Tags");
            TagButtonComboBox.ItemsSource = from t in photoEntities.Tags select t.TagName;
          
        }

        private void OnClose(object sender, EventArgs e)
        {
        }

    }
}